package com.gxa.modules.payment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.payment.entity.MeterCycle;
import com.gxa.modules.payment.entity.PayItem;
import com.gxa.modules.payment.form.MeterCycleForm;
import com.gxa.modules.payment.form.MeterCycleQueryForm;
import com.gxa.modules.payment.service.MeterCycleService;
import com.gxa.modules.payment.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "抄表周期管理 接口")
@RestController
public class MeterCycleController {

    @Autowired
    private MeterCycleService meterCycleService;

    @ApiOperation("抄表周期管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),
            @ApiImplicitParam(paramType = "query",name = "meterName", value = "查询条件 仪表名称"),
    })
    @GetMapping("/meterCycle/list")
    public Result<List<MeterCycle>> queryByPage(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<MeterCycle>> r = Result.success();

        String page1 = (String) params.get("page");
        String limit1 = (String) params.get("limit");

        Integer page = Integer.parseInt(page1);
        Integer limit = Integer.parseInt(limit1);

        try{
            PageHelper.startPage(page,limit);
            List<MeterCycle> meterCycles = this.meterCycleService.queryByPage(params);
            PageInfo<MeterCycle> pageInfo = new PageInfo<>(meterCycles);

            long total = (int)pageInfo.getTotal();
            return Result.success(meterCycles,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

//    @ApiOperation("生成 仪表编号")
//    @GetMapping("/meterCycle/addNo")
//    public Result addNo(){
//        Result r = Result.failed();
//        try{
//            Integer receiptNo = this.meterCycleService.addNo();
//            r = Result.success(receiptNo);
//        } catch (Exception e){
//            r.setCode("1");
//            r.setMsg("error");
//            e.printStackTrace();
//        }
//        return r;
//    }

    @ApiOperation("抄表周期管理 添加前查询小区")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "cycleId",value = "查询条件 cycleId"),
    })
    @GetMapping("/meterCycle/queryById")
    public Result queryById(@RequestParam Integer cycleId){
        Result r = Result.success();

        try{
            String cellName = this.meterCycleService.queryByCellName(cycleId);

            r = Result.success(cellName);
            return r;
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("抄表周期管理 添加")
    @PostMapping("/meterCycle/add")
    public Result addMeterCycle(@RequestBody MeterCycleForm meterCycleForm){
        System.out.println(meterCycleForm);
        Result r = new Result<>();

        try{
            this.meterCycleService.meterCycleAdd(meterCycleForm);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("抄表明细 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),
            @ApiImplicitParam(paramType = "query",name = "cycleId", value = "查询条件 抄表周期 ID"),
    })
    @GetMapping("/meterCycle/queryMeterDetails")
    public Result<List<MeterCycleQueryForm>> queryMeterDetails(@RequestParam Integer cycleId,Integer page,Integer limit){
        Result<List<MeterCycleQueryForm>> r = Result.success();

        try{
            r= this.meterCycleService.queryMeterDetails(cycleId, page, limit);

        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("抄表周期管理 删除")
    @DeleteMapping("/meterCycle/delete")
    public Result deleteMeterCycle(@RequestParam Integer id){
        Result r = new Result<>();

        try{
            this.meterCycleService.meterCycleDelete(id);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("收费项目管理 删除多条")
    @DeleteMapping("/meterCycle/ids/delete")
    public Result payItemDeletes(@RequestBody List<Integer> ids){
        Result r = Result.success();

        try{
            this.meterCycleService.meterCycleDeleteIds(ids);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
